
package com.hwhp.web.console.action;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.hwhp.common.BaseAction;
import com.hwhp.common.Pager;
import com.hwhp.entity.Deptment;
import com.hwhp.service.console.DepartService;


/**
 * @author lyf
 *Jun 25, 2009
 * desc: curd action
 */
public class AdminDepart extends BaseAction{
	private DepartService departService = null ;
	/**
	 * 部门管理初始
	 * add by zyc
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String departIndex() {
		//return mapping.findForward("departmanager.init.success");
		return "departmanager.init.success" ;
	}
	/**
	 * 部门组织机构
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String departTree() {
		
		HttpServletRequest request = this.getHttpRequest();
		String deptid=request.getParameter("deptid");
		
		List<Deptment> list = departService.getChildDepts(deptid);
		
		request.setAttribute("deptList", list);
		//return mapping.findForward("departmanager.tree.success");
		return "departmanager.tree.success";
	}
	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String departList() {
		HttpServletRequest request = this.getHttpRequest();
		String deptid=request.getParameter("parentid");
		String act = request.getParameter("act");
		String pageNumber = request.getParameter("pageNumber");
		String title = request.getParameter("title");
		if(title == null){
			title = "";
		}
		int pgNo = 1;
		try{
			pgNo = Integer.parseInt(pageNumber);
		}catch(Exception e){
			pgNo = 1;
			e.printStackTrace();
		}
		List<Deptment> list = new ArrayList<Deptment>();
		Pager page = new Pager();
		if(deptid==null||"".equals(deptid)||"null".equals(deptid)){
			deptid="-1";
		}
		//if("search".equals(act)){//如果是查询操作
			
			page = departService.queryDepts(title, deptid,pgNo);
			request.setAttribute("page",page);
//		}else{
//			list = departService.getChildDepts(deptid);
//		}
		//System.out.println("list size :"+list.size());
		request.setAttribute("title", title);
		request.setAttribute("subdeptList", list);
		request.setAttribute("parentid", deptid);
		//return mapping.findForward("departmanager.list.success");
		return "departmanager.list.success";
	}
	/**
	 * 部门编辑
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String departEdit() {
		HttpServletRequest request = this.getHttpRequest();
		String parentid=request.getParameter("parentid");
		String deptid=request.getParameter("deptid");
		String act=request.getParameter("act");
		if(act!=null&&"add".equals(act)){
			Deptment parentObj = departService.getDepart(parentid);
			Deptment deptObj=new Deptment();
			request.setAttribute("deptObj", deptObj);
			request.setAttribute("parentObj", parentObj);
			request.setAttribute("act","add");
		}
		if(act!=null&&"edit".equals(act)){
			Deptment deptObj=departService.getDepart(deptid);
			Deptment parentObj=departService.getDepart(deptObj.getParentId());
			request.setAttribute("deptObj", deptObj);
			request.setAttribute("parentObj", parentObj);
			request.setAttribute("act","edit");
		}
		//return mapping.findForward("departmanager.edit.success");
		return "departmanager.edit.success";
	}
	/**
	 * 新增部门
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String insertDepart() {
		HttpServletRequest request = this.getHttpRequest();
		String act=request.getParameter("act");
		Deptment dept = new Deptment();
		boolean success = false ;
		String deptName = request.getParameter("departname");
		String parentId = request.getParameter("parentid");
		//System.out.println("parendId:"+parentId);
		String deptid=request.getParameter("departid");
		if(parentId == null||"".equals(parentId)||"null".equals(parentId)){
			parentId="-1";
		}
		String departCode = request.getParameter("departcode");
		if("add".equals(act)){
			dept.setParentId(parentId);
			dept.setDeptName(deptName);
			dept.setDeptCode(departCode);
			success = departService.insertDepart(dept);
		}
		if("edit".equals(act)){
			//dept=departService.getDepart(deptid);
			dept.setDeptId(deptid);
			dept.setParentId(parentId);
			dept.setDeptName(deptName);
			dept.setDeptCode(departCode);
			success = departService.editDepart(dept);
			request.setAttribute("deptObj", dept);
		}
		if(success){
			Deptment parentObj=departService.getDepart(parentId);
			request.setAttribute("parentObj", parentObj);
			request.setAttribute("act",act);
			request.setAttribute("success", "true");
		}else{
			request.setAttribute("success", "false");
		}
		//return mapping.findForward("departmanager.edit.success");
		return "departmanager.edit.success" ;
	}
	public String editDepart() {
		HttpServletRequest request = this.getHttpRequest();
		boolean success = false ;
		Deptment dept = new Deptment();
		String deptName = request.getParameter("deptName");
		String parentId = request.getParameter("parentId");
		String deptId = request.getParameter("deptId");
		dept.setParentId(parentId);
		dept.setDeptName(deptName);
		dept.setDeptId(deptId);
		
		success = departService.editDepart(dept);
		if(success){
			request.setAttribute("success", "true");
		}else{
			request.setAttribute("success", "false");
		}
		//return mapping.findForward("success");
		return "success";
	}
	/**
	 * 删除部门
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String deleteDepart() {
		boolean success = false ;
		//DepartService deptService = new DepartService();
		HttpServletRequest request = this.getHttpRequest();
		Deptment dept = new Deptment();
		List<Deptment> subList=new ArrayList();
		String deptId =request.getParameter("deptid");
		dept = departService.getDepart(deptId);
		subList=departService.getChildDepts(deptId);
		try {
			success = departService.deleteDepart(dept);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(Deptment subDept:subList){
			try {
				success = departService.deleteDepart(subDept);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return this.departList();
	}
	
	
	public void setDepartService(DepartService departService) {
		this.departService = departService;
	}
	public DepartService getDepartService() {
		return departService;
	}
	
}
